package io.renren.modules.yckj.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.yckj.dao.CouponsDao;
import io.renren.modules.yckj.dto.CouponsDTO;
import io.renren.modules.yckj.entity.CouponsEntity;
import io.renren.modules.yckj.service.CouponsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 优惠券管理
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2025-04-16
 */
@Service
public class CouponsServiceImpl extends CrudServiceImpl<CouponsDao, CouponsEntity, CouponsDTO> implements CouponsService {

    @Override
    public QueryWrapper<CouponsEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        Object type = params.get("type");
        Object distributionType = params.get("distributionType");
        Object status = params.get("status");
        Object title = params.get("title");

        QueryWrapper<CouponsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.eq(ObjectUtil.isAllNotEmpty(type), "type", type);
        wrapper.eq(ObjectUtil.isAllNotEmpty(distributionType), "distribution_type", distributionType);
        wrapper.eq(ObjectUtil.isAllNotEmpty(status), "status", status);
        wrapper.like(ObjectUtil.isAllNotEmpty(title), "title", title);

        return wrapper;
    }


    /**
     * @param params 条件
     * @return 分页数据
     */
    @Override
    public PageData<CouponsDTO> pageForApi(Map<String, Object> params) {
        //分页
        IPage<CouponsEntity> page = getPage(params, "id", false);
        //查询
        List<CouponsEntity> list = baseDao.getCouponsList(params);
        return getPageData(list, page.getTotal(), CouponsDTO.class);
    }


}